import useConfigStore from "@/store/modules/company.js";
import useUserStore from "@/store/modules/user.js";

//获取所有公司列表
export function getCompany(){
    const list = useConfigStore().companyList;
    if (list.length > 0){
     return   list;
    }else {
       return useConfigStore().getCompanyList().then(resp =>{
            return resp.data;
        })
    }
}
//获取公司名称
export function getCompanyName(id){
    let list = getCompany();
    if (list != null){
        for (let i= 0;i < list.length;i++){
            if (parseInt(id) === list[i].deptId){
                return list[i].deptName;
            }
        }
    }
}

//获取所有账户类型
export function getAccountType(){
    const list = useConfigStore().accountTypeList;
    if (list.length > 0){
        return   list;
    }else {
        return useConfigStore().getAccountTypeList().then(resp =>{
            return resp.data;
        })
    }
}

//获取账户性质名
export function getAccountTypeName(id){
    let list = getAccountType();
    if (list != null){
        for (let i = 0; i < list.length; i++) {
            if (list[i].accountTypeId === parseInt(id)){
                return list[i].accountTypeName;
            }
        }
    }

}
//获取当前账号所拥有公司列表
export function getOwnCompany(){
    let list = getCompany();
    let target = [];
    let data = [];
    target.push(useUserStore().companyId);
    target.push(...useUserStore().underCompany);
    if (list != null){
        for (let i = 0; i < list.length;i++){
            for (let j = 0;j < target.length;j++){
                if (list[i].deptId === target[j]){
                    data.push(list[i]);
                }
            }
        }
        return data;
    }
}

